package com.codi.bus.core.service.organ.impl;

import com.codi.bus.core.dao.OrganAgentDao;
import com.codi.bus.core.dao.OrganProfileDao;
import com.codi.bus.core.dao.TradeNoticeDao;
import com.codi.bus.core.domain.OrganAgent;
import com.codi.bus.core.domain.OrganProfile;
import com.codi.bus.core.domain.TradeNotice;
import com.codi.bus.core.service.organ.OrganMineService;
import com.codi.bus.exception.BaseException;
import com.codi.bus.util.NumberUtil;
import com.codi.message.constant.ErrorConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author lvgo
 * @version 1.0
 * @Description: 机构用户
 * @date 2018/5/9 10:07
 */
@Service
@Slf4j
public class OrganMineServiceImpl implements OrganMineService {

    @Autowired
    private TradeNoticeDao tradeNoticeDao;

    @Autowired
    private OrganProfileDao organProfileDao;

    @Autowired
    private OrganAgentDao organAgentDao;

    @Override
    public TradeNotice saveTradeNotice(String userId, String profileId, String mobile, String name) {

        OrganAgent organAgent = organAgentDao.getEntityByMobile(mobile);
        if (organAgent != null) {
            throw new BaseException(ErrorConstant.INSERT_NOT_AGENT_MIBILE);
        }
        TradeNotice notice = tradeNoticeDao.getByMobile(profileId, mobile);
        if (notice != null) {
            throw new BaseException(ErrorConstant.TRADE_NOTICE_MOBILE_EXIST);
        }
        boolean flag = NumberUtil.isMobilePhoneVerify(mobile);
        if (!flag) {
            throw new BaseException(ErrorConstant.INSERT_CORRECT_TRADE_NOTICE_MOBILE);
        }
        OrganProfile organProfile = organProfileDao.selectByPrimaryKey(Long.valueOf(profileId));
        if (organProfile == null) {
            throw new BaseException(ErrorConstant.ORGAN_PROFILE_NULL);
        }
        if (!organProfile.getUserId().toString().equals(userId)) {
            throw new BaseException(ErrorConstant.USER_MATCHING_ORGAN);
        }
        TradeNotice tradeNotice = new TradeNotice();
        tradeNotice.setCreateTime(new Date());
        tradeNotice.setCreateUserId(Integer.valueOf(userId));
        tradeNotice.setNoticeMobile(mobile);
        tradeNotice.setProfileId(Long.valueOf(profileId));
        tradeNotice.setNoticeName(name);
        int i = tradeNoticeDao.insert(tradeNotice);
        if (i != 1) {
            throw new BaseException(0, "保存交易报备号失败");
        }
        return tradeNotice;
    }

    /**
     * 删除交易报备号
     *
     * @param tradeNoticeId 交易报备号id
     */
    @Override
    public void deleteTradeNotice(String tradeNoticeId) {
        int i = tradeNoticeDao.deleteByPrimaryKey(Long.valueOf(tradeNoticeId));
        if (i != 1) {
            log.error("删除交易报备号失败，错误信息：{}", "数据库执行" + i + "条记录");
            throw new BaseException("删除交易报备号失败，未找到该条数据记录");
        }
    }

    /**
     * 查询交易报备号列表
     *
     * @param profileId
     * @return
     */
    @Override
    public List<TradeNotice> selectTradeNoticeList(String profileId) {
        return tradeNoticeDao.getListByProfileId(profileId);
    }

    @Override
    public OrganAgent getAgent(String profileId) {
        OrganProfile organProfile = organProfileDao.selectByPrimaryKey(Long.valueOf(profileId));
        if (organProfile == null) {
            throw new BaseException(ErrorConstant.ORGAN_PROFILE_NULL);
        }
        Long agentId = organProfile.getOrganAgentId();
        OrganAgent organAgent = organAgentDao.selectByPrimaryKey(agentId);
        if (organAgent == null) {
            return new OrganAgent();
        } else {
            return organAgent;
        }
    }
}
